package com.stsolutions.budgetcontroller;

import java.util.List;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.stsolutions.budgetcontroller.db.DaoObject;
import com.stsolutions.budgetcontroller.db.DataBaseActions;
import com.stsolutions.budgetcontroller.db.DatabaseHelper;
import com.stsolutions.budgetcontroller.model.data.CostBucket;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class BudgetControllerActivity extends
		OrmLiteBaseActivity<DatabaseHelper> implements OnClickListener,
		OnItemClickListener {
	protected final String LOG_TAG = getClass().getSimpleName();
	private Button newCostBucketBtn;
	private Button deleteCostBucketBtn;
	private Button showCostsBtn;

	// TODO should show list on create, add new bucket to list of buckets, show
	// available list of buckets
	private ListView costBucketListView;
	private ArrayAdapter<CostBucket> adapter;
	private List<CostBucket> adapterItems;

	private CostBucket selectedCostBucket = null;

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		costBucketListView = (ListView) findViewById(R.id.myList);
		costBucketListView.setOnItemClickListener(this);
		adapterItems = (List<CostBucket>) getHelper().doDataBaseAction(
				DataBaseActions.ACTION_GET_ALL, adapterItems, null,
				DaoObject.COST_BUCKET);
		initializeCostBucketListView();

		newCostBucketBtn = (Button) findViewById(R.id.newCostBucketBtn);
		deleteCostBucketBtn = (Button) findViewById(R.id.deleteCostBucketBtn);
		showCostsBtn = (Button) findViewById(R.id.showCostsBtn);

		newCostBucketBtn.setOnClickListener(this);
		deleteCostBucketBtn.setOnClickListener(this);
		showCostsBtn.setOnClickListener(this);
	}

	private void initializeCostBucketListView() {
		// First paramenter - Context
		// Second parameter - Layout for the row
		// Third parameter - ID of the TextView to which the data is written
		// Forth - the Array of data
		adapter = new ArrayAdapter<CostBucket>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1,
				adapterItems);
		// Assign adapter to ListView
		costBucketListView.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.newCostBucketBtn: {
			Intent newBudgetControl = new Intent(this,
					NewBudgetControllActivity.class);
			startActivity(newBudgetControl);
			break;
		}
		case R.id.deleteCostBucketBtn: {
			if (selectedCostBucket != null)
				createDeleteDialog();

			break;
		}
		case R.id.showCostsBtn: {
			if (selectedCostBucket != null) {
				Intent showCostsWindow = new Intent(this,
						ShowCostsActivity.class);
				showCostsWindow.putExtra("costBucketSelected",
						selectedCostBucket.getCostBucketId());
				startActivity(showCostsWindow);
			}
			break;
		}
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		CostBucket bucket = (CostBucket) parent.getItemAtPosition(position);
		Log.i(LOG_TAG, "int position = " + position);
		Log.i(LOG_TAG, "cost bucket id = " + bucket.getCostBucketId());
		selectedCostBucket = bucket;
	}

	private void createDeleteDialog() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Delete CostBucket");
		alert.setMessage("Confirm delete:\n\n" + selectedCostBucket);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				getHelper().doDataBaseAction(
						DataBaseActions.ACTION_DELETE_ITEM, selectedCostBucket,
						adapterItems, DaoObject.COST_BUCKET);
				adapter.notifyDataSetChanged();
			}
		});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {

					}
				});
		alert.show();

	}

}